<?php

class ListAction extends CAction
{
    const PAGE_SIZE=3;

    public function run()
    {
        if(!isset($_GET['id']))
            throw new CHttpException(400, 'The request is invalid');
    
        $user=User::loadObjectById($_GET['id']);
        if($user===null)
            throw new CHttpException(404,'The requested user does not exist.');

        $kitchen=Kitchen::loadObjectById($user->id, array('recipeNum','publishedRecipeNum'));
        if($kitchen===null)
            throw new CHttpException(404,'The requested kitchen does not exist.');
            
        $pages=new CPagination();
        $pages->pageSize=self::PAGE_SIZE;
        $criteria=new CDbCriteria;
        
        if(Yii::app()->user->id==$user->id) {
            $pages->itemCount=$kitchen->recipeNum;
            $pages->applyLimit($criteria);
            $recipes = Recipe::loadObjectsByUserId($user->id, $criteria);
        }
        else {
            $pages->itemCount=$kitchen->publishedRecipeNum;
            $pages->applyLimit($criteria);
            $recipes = Recipe::loadPublishedObjectsByUserId($user->id, $criteria);
        }
    
        $this->controller->render('list',array(
            'user'=>$user,
            'kitchen'=>$kitchen,
            'recipes'=>$recipes,
            'pages'=>$pages,
        ));
    }
}

?>